"""
这是ubutnu 环境下spyder中调教dmrg的全套代码
要求已经按照了alps，使用Python 2.7运行
代码分为两部分，运行dmrg和分析dmrg产生的数据，可以注释一个运行另一个，也可以两个都运行
运行后者需要和前者产生的alps结果文件在一起
"""
# encoding: utf-8  
## if you are using spyder, run these:
from IPython import get_ipython
get_ipython().magic('reset -sf') 

import sys
sys.path.append('/opt/alps/lib')
import pyalps
import matplotlib.pyplot as plt
import pyalps.plot
import numpy as np
import time

#----------------------1 Run the Task-------------------
#a1=time.time()
#BHparms = []  ## 用dictionary作为元素组成list
#U=0.759882932623722
#for m in [0.5*U, 1.5*U, 0.7*U, 1.7*U]:
#    BHparms.append(
#        {
#        'LATTICE':"open chain lattice",
#        'L':6,  ## L也是字符串啊，6
#        
#        'MODEL_LIBRARY':"bhmodel.xml", ## 双引号说不定又和models文件中一样有区别
#        'MODEL':"my boson hubbard",
#
#        'J':0.004383498897471,   ## 数字就输数字，不用字符串
#        'U':U,  
#        'mu':m,
#        'Nmax':5,
#        'N_total':10,
#
#        'MAXSTATES':128,
#        'SWEEPS':10,
#        'MEASURE_LOCAL[expectation_n]': 'n(i)',
#        'MEASURE_LOCAL[expectation_n^2]':'n(i)*n(i)'
#        })
#input_file = pyalps.writeInputFiles('vhigh50py', BHparms)
#pyalps.runApplication('dmrg', input_file, writexml=True, MPI=4)
#a2=time.time()
#print('rundmrg has spent %.1f seconds' % (a2-a1))

#----------------------2 结果分析--------------------------

result_files = pyalps.getResultFiles(prefix='vhigh50py')
#print(result_files)
#eigen_measure = pyalps.loadEigenstateMeasurements(result_files)
eigen_measure = pyalps.loadEigenstateMeasurements(result_files,'expectation_n')
gather = pyalps.collectXY(eigen_measure,'mu','expectation_n')
print(gather)
#e=eigen_measure[0][0]
#plotdata1 = pyalps.collectXY(eigen_measure,'mu','n(i)')
#print(plotdata1)
#plotdata2 = pyalps.collectXY(measurement_data,'T','Energy')
#x1=plotdata1[0].x
#y1=np.array([i.mean for i in plotdata1[0].y])
#x2=plotdata2[0].x
#y2=np.array([i.mean for i in plotdata2[0].y])
#
#fig=plt.figure()
#(fig,ax)=plt.subplots(figsize=(4,3),dpi=100)
#ax.plot(x1,y1,'b',label='|Magneti|')
#ax.plot(x2,y2,'--',color='purple',label='Energy')
#ax.legend(loc=4)
#ax.set_xlabel('x')
#ax.set_ylabel('y')
#ax.set_title('test tu')
#fig.savefig('dema.pdf')